package Controller

import (
	"encoding/json"
	"fmt"
	"github.com/gin-gonic/gin"
	"gorm.io/gorm"
	"log"
	"m/Global"
	"m/model"
	"net/http"
)

func RegisterEvent(context *gin.Context) {
	// 使用结构体来获取参数
	var requsetEvent = model.Event{}
	json.NewDecoder(context.Request.Body).Decode(&requsetEvent)
	fmt.Println("获取到的数据：", requsetEvent.Eno, requsetEvent.Ename, requsetEvent.Elevel, requsetEvent.Echaracter, requsetEvent.Eaddress, requsetEvent.Edie, requsetEvent.Emarry, requsetEvent.Epartner, requsetEvent.Elife, requsetEvent.Ebirth, requsetEvent.Echild, requsetEvent.Ehonor)

	// 获取参数
	fmt.Println("开始获取教师参数...")
	eno := requsetEvent.Eno
	ename := requsetEvent.Ename
	elevel := requsetEvent.Elevel
	echaracter := requsetEvent.Echaracter
	eaddress := requsetEvent.Eaddress
	edie := requsetEvent.Edie
	emarry := requsetEvent.Emarry
	epartner := requsetEvent.Epartner
	elife := requsetEvent.Elife
	ebirth := requsetEvent.Ebirth
	echild := requsetEvent.Echild
	ehonor := requsetEvent.Ehonor

	// 数据验证
	fmt.Println("开始验证家谱数据...")
	fmt.Println("家谱编号：", eno, "家谱名字：", ename, "家谱等级：", elevel, "家谱特征：", echaracter, "族长联系方式：", eaddress, "家训：", edie, "结婚日期：", emarry, "配偶信息：", epartner, "生平事迹：", elife, "出生日期：", ebirth, "子女信息：", echild, "生前荣誉：", ehonor)
	if len(eno) == 0 {
		//context.JSON(http.StatusUnprocessableEntity, gin.H{"code":421, "msg":"家谱号不可为空！"})
		fmt.Println("发现家谱编号为空")
		context.JSON(200, gin.H{
			"code": 200,
			"msg":  "家谱编号不能为空",
		})
		return
	}
	//if len(ename) == 0 {
	//	//context.JSON(http.StatusUnprocessableEntity, gin.H{"code":422, "msg":"家谱名不可为空！"})
	//	fmt.Println("发现家谱名字为空")
	//	context.JSON(200, gin.H{
	//		"code": 200,
	//		"msg":  "家谱名字不能为空",
	//	})
	//	return
	//}
	fmt.Println(eno, ename, elevel, echaracter, eaddress)
	log.Println(eno, ename, elevel, echaracter, eaddress)
	// 判断教师号是否存在
	//tempEno, err := strconv.Atoi(eno)
	if isEnoExists(Global.Db, eno) == true {
		//context.JSON(http.StatusUnprocessableEntity, gin.H{"code":422, "msg":"教师号已经存在！"})
		// 直接返回，不执行下面的create了
		context.JSON(200, gin.H{
			"code": 200,
			"msg":  "成员编号已经存在",
		})
		return
	}
	// 创建成员
	//if err != nil {
	//	fmt.Println("错误")
	//	return
	//}
	newEvent := model.Event{
		Eno:        eno,
		Ename:      ename,
		Elevel:     elevel,
		Echaracter: echaracter,
		Eaddress:   eaddress,
		Edie:       edie,
		Emarry:     emarry,
		Epartner:   epartner,
		Elife:      elife,
		Ebirth:     ebirth,
		Echild:     echild,
		Ehonor:     ehonor,
	}

	//Global.Db.Create(&newEvent)
	//// 返回结果
	//context.JSON(200, gin.H{
	//	"msg": "插入成功",
	//})

	if err := Global.Db.Create(&newEvent).Error; err != nil {
		context.JSON(201, gin.H{
			"code": 201,
			"msg":  "插入事件失败",
		})
	} else {
		context.JSON(200, gin.H{
			"code": 200,

			"msg": "插入事件成功",
		})
	}

}

func ShowEvent(context *gin.Context) {
	fmt.Println("进入showevent")
	var event []model.Event
	err := Global.Db.Find(&event).Error
	if err != nil {
		fmt.Println("查询事件出错！")
		return
	}
	fmt.Println("数据库查到的事件信息：", event)

	//token, err := Global.ReaeaseTokec3(event)
	context.JSON(http.StatusOK, event)
}

func CountEvent(context *gin.Context) {
	// 调用存储过程
	var count int
	Global.Db.Raw("select count_event()").First(&count)
	fmt.Println("事件数量：", count)
	context.JSON(200, gin.H{
		"code": 200,
		"msg":  "返回家族成员数量成功",
		"data": count,
	})
}

func EditEvent(context *gin.Context) {
	fmt.Println("进入editevent")
	var requestEvent = model.Event{}
	json.NewDecoder(context.Request.Body).Decode(&requestEvent)
	fmt.Println("即将编辑家族成员编号：", requestEvent.Eno)

	if len(requestEvent.Eno) < 1 {
		fmt.Println("家族成员编号不能为空！", len(requestEvent.Eno))
		context.JSON(200, gin.H{
			"code": 200,
			"msg":  "家族成员编号不能为空",
		})
		return
	}
	//if len(requestEvent.Ename) < 1 {
	//	fmt.Println("成员姓名不能为空！", len(requestEvent.Ename))
	//	context.JSON(200, gin.H{
	//		"code": 200,
	//		"msg":  "成员姓名不能为空",
	//	})
	//	return
	//}
	//if len(requestEvent.Zlevel) < 1 {
	//	fmt.Println("家族成员等级不能为空！", len(requestEvent.Zlevel))
	//	context.JSON(200, gin.H{
	//		"code": 200,
	//		"msg":  "家族成员等级不能为空",
	//	})
	//	return
	//}
	//if len(requestEvent.Zcharacter) < 1 {
	//	fmt.Println("成员特长不能为空！", len(requestEvent.Zcharacter))
	//	context.JSON(200, gin.H{
	//		"code": 200,
	//		"msg":  "成员特长不能为空",
	//	})
	//	return
	//}

	//// 教师号是否存在
	//var event model.Event
	//Global.Db.Where("eno = ?", requestEvent.Eno).First(&event)
	//fmt.Println("event.Eno=", event.Eno)
	//if event.Eno == "" {
	//	context.JSON(200, gin.H{
	//		"code": 200,
	//		"msg":  "找不到此家族成员",
	//	})
	//	return
	//}
	//Global.Db.Save(&requestEvent)
	//Global.Db.Model(&requestEvent).Updates(model.Event{
	//	Eno: requestEvent.Eno,
	//	Ename: requestEvent.Ename,
	//	Elevel: requestEvent.Elevel,
	//	Echaracter: requestEvent.Echaracter,
	//})
	err := Global.Db.Where("eno = ?", requestEvent.Eno).Updates(&requestEvent).Error
	if err != nil {
		fmt.Println("更新成员出错")
		context.JSON(200, gin.H{
			"code": 200,
			"msg":  "更新成员出错",
		})
		return
	}

	context.JSON(200, gin.H{
		"code": 200,
		"msg":  "更新成功",
	})

}

func DeleteEvent(context *gin.Context) {
	fmt.Println("进入deleteevent")
	var requestEvent = model.Event{}
	json.NewDecoder(context.Request.Body).Decode(&requestEvent)
	fmt.Println("即将删除成员编号：", requestEvent.Eno)

	if len(requestEvent.Eno) < 1 {
		fmt.Println("成员编号不能为空！", len(requestEvent.Eno))
		context.JSON(200, gin.H{
			"code": 200,
			"msg":  "成员编号不能为空",
		})
		return
	}
	// 教师号是否存在
	var event model.Event
	Global.Db.Where("eno = ?", requestEvent.Eno).First(&event)
	fmt.Println("event.eno=", event.Eno)
	if event.Eno == "" {
		context.JSON(200, gin.H{
			"code": 200,
			"msg":  "找不到此成员",
		})
		return
	}
	// 首先删除有这个老师的课程
	var relation = model.Relation{
		Eno: requestEvent.Eno,
	}
	var err error
	err = Global.Db.Where("eno = ?", requestEvent.Eno).Delete(&relation).Error
	if err != nil {
		context.JSON(200, gin.H{
			"code": 200,
			"msg":  "删除此教师所教授的课程出错",
		})
		return
	}

	// 其次删除这个老师
	err = Global.Db.Where("eno = ?", requestEvent.Eno).Delete(&event).Error
	if err != nil {
		context.JSON(200, gin.H{
			"code": 200,
			"msg":  "删除事件出错",
		})
		return
	}

	context.JSON(200, gin.H{
		"code": 200,
		"msg":  "删除成功",
	})
}

func ShowEventSelect(context *gin.Context) {
	fmt.Println("进入showteacheerselect")
	var requestEvent = model.Event{}
	json.NewDecoder(context.Request.Body).Decode(&requestEvent)
	fmt.Println("即将搜索成员编号：", requestEvent.Eno)

	if len(requestEvent.Eno) < 1 {
		fmt.Println("成员编号不能为空！", len(requestEvent.Eno))
		context.JSON(http.StatusUnprocessableEntity, gin.H{
			"code": 422,
			"msg":  "成员编号为空",
		})
		return
	}
	// 家族成号是否存在
	var event []model.Event
	Global.Db.Where("eno = ?", requestEvent.Eno).First(&event)
	fmt.Println("event.eno=", event[0].Eno)
	if event[0].Eno == "" {
		context.JSON(http.StatusUnprocessableEntity, gin.H{
			"code": 422,
			"msg":  "找不到此家族成员！",
		})
		return
	}
	context.JSON(http.StatusOK, event)

}

func ShowEventDelete(context *gin.Context) {
	fmt.Println("进入showeventdelete")
	//var requesetEvent = model.Event{}
}

func isEnoExists(db *gorm.DB, eno string) bool {
	var event model.Event
	err := db.Where("eno = ?", eno).Find(&event).Error
	if err != nil {
		fmt.Println("err执行")
		return false
	}
	fmt.Println("数据库返回的教师号：", event.Eno)
	if event.Eno != "" { // 找到重复的教师号
		fmt.Println("找到重复的教师号为：", event.Eno)
		fmt.Println("成员执行了if")
		return true
	} else {
		fmt.Println("成员执行了else")
		return false
	}
}
